package uk.ac.aber.paws.client.util;

import java.util.*;

import uk.ac.aber.paws.client.config.ConfigManager;
import uk.ac.aber.paws.client.core.PAWSClient;

/**
 * @author Rob Johnson, Aberystwyth University
 * @email rob.johnson@aber.ac.uk
 * @date 16th August 2011
 * 
 * TimeChecks.java
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

public class TimeChecks {

	/**
	 * 
	 * @return Returns true if current time is within the specified immunity period
	 */
	public static boolean inImmunityPeriod() {
	
		// get the current time, extract the current hour and mins
		Calendar now = Calendar.getInstance();
		Calendar startBound = Calendar.getInstance();
		Calendar endBound = Calendar.getInstance();
			
		String period = PAWSClient.getConfig().getProperty("ALL_COMPUTER_IMMUNITY_PERIOD");
		
		// the time should be in the format HH:MM-HH:MM to be valid
		
		// split into start time and end time
		StringTokenizer st = new StringTokenizer(period,"-");
		
		int startHour, startMin, endHour, endMin;
		
		if (st.countTokens() == 2) {
			
			String startTime = st.nextToken();
			String endTime = st.nextToken();
			
			// For the start time...
			
			// split into hours and minutes
			st = new StringTokenizer(startTime,":");
			
			if (st.countTokens() == 2) {
				
				// set the calendar instance
				startHour = Integer.parseInt(st.nextToken());
				startMin = Integer.parseInt(st.nextToken());
				startBound.set(Calendar.HOUR_OF_DAY, startHour);
				startBound.set(Calendar.MINUTE, startMin);
				startBound.set(Calendar.SECOND, 0);
				
			}
			else {
				return true;
			}
			
			//For the end time...
			
			// split into hours and minutes
			st = new StringTokenizer(endTime,":");
			
			if (st.countTokens() == 2) {
				
				// set the calendar instance...
				endHour = Integer.parseInt(st.nextToken());
				endMin = Integer.parseInt(st.nextToken());
				endBound.set(Calendar.HOUR_OF_DAY, endHour);
				endBound.set(Calendar.MINUTE, endMin);
				endBound.set(Calendar.SECOND, 0);
			}
			else {
				return true;
			}
			
			// check if the current time falls between the start and end times
			if (now.getTimeInMillis() > startBound.getTimeInMillis() && now.getTimeInMillis() < endBound.getTimeInMillis()){
				return true;	
			}
			else {
				return false;
			}
		}
		else {
			return true;
		}
	}
	
}
